package com.store.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.CommissionMembersAdapter;
import com.store.app.bean.CommissionMemberBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 佣金-会员数 页面。
 * Created by Liam on 2018/3/30.
 */

public class CommissionMembersActivity extends BaseActivity
        implements IOnHttpListener, View.OnClickListener {

    private static final int REQ_COMMISSION_MEMBERS_REFRESH = 1;
    private static final int REQ_COMMISSION_MEMBERS_LOAD_MORE = 2;

    private ImageView ivReturn;
    private TextView title;
    private TextView right;
    private AutoListView autoListView;

    private int pageNo;
    private NetRequest netRequest;
    private List<CommissionMemberBean> list = new ArrayList<>();
    private CommissionMembersAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commission_members);
        ExitApplication.getInstance().addActivity(this);
        init();
        initView();
        initClicks();
        initData();
    }

    private void initData() {
        // init ListView.
        if (list == null) list = new ArrayList<>();

        adapter = new CommissionMembersAdapter(this, list);
        autoListView.setAdapter(adapter);
        autoListView.setOnRefreshListener(new AutoListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                netRequest.requestCommissionMembersList(REQ_COMMISSION_MEMBERS_REFRESH, pageNo);
                autoListView.onRefreshComplete();
            }
        });
        autoListView.setOnLoadListener(new AutoListView.OnLoadListener() {
            @Override
            public void onLoad() {
                netRequest.requestCommissionMembersList(REQ_COMMISSION_MEMBERS_LOAD_MORE, pageNo);
                autoListView.onLoadComplete();
            }
        });
        autoListView.onRefresh();
    }

    private void initClicks() {
        // title bar.
        title.setText("会员列表");
        ivReturn.setOnClickListener(this);
        right.setBackgroundResource(R.drawable.super_return_help);
        right.setOnClickListener(this);
    }

    private void initView() {
        ivReturn = (ImageView) findViewById(R.id.public_ll_return);
        title = (TextView) findViewById(R.id.tvTitle);
        right = (TextView) findViewById(R.id.public_tv_right);
        autoListView = (AutoListView) findViewById(R.id.lv_commission_members_list);
    }

    private void init() {
        pageNo = 1;
        netRequest = new NetRequest(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.public_tv_right:
                Intent intent = new Intent(CommissionMembersActivity.this, WebViewMoreActivity.class);
                intent.putExtra("url", "http://help.meitianhui.com/?id=1009");
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == REQ_COMMISSION_MEMBERS_REFRESH) {
            try {
                JSONObject data = new JSONObject(result);
                JSONArray jsonArray = data.getJSONArray("list");

                Gson gson = new Gson();
                List<CommissionMemberBean> list = new ArrayList<>();
                CommissionMemberBean temp;
                for (int i = 0; i < jsonArray.length(); i++) {
                    temp = gson.fromJson(jsonArray.getJSONObject(i).toString(), CommissionMemberBean.class);
                    list.add(temp);
                }
                autoListView.onRefreshComplete();

                if (data.getJSONObject("page").getInt("page_no") == pageNo) {
                    autoListView.setResultSize(list.size());
                    this.list.clear();
                    this.list.addAll(list);
                    adapter.notifyDataSetChanged();
                    pageNo += 1;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else if (requestId == REQ_COMMISSION_MEMBERS_LOAD_MORE) {
            try {
                JSONObject data = new JSONObject(result);
                JSONArray jsonArray = data.getJSONArray("list");

                Gson gson = new Gson();
                List<CommissionMemberBean> list = new ArrayList<>();
                CommissionMemberBean temp;
                for (int i = 0; i < jsonArray.length(); i++) {
                    temp = gson.fromJson(jsonArray.getJSONObject(i).toString(), CommissionMemberBean.class);
                    list.add(temp);
                }
                autoListView.onLoadComplete();

                if (data.getJSONObject("page").getInt("page_no") == pageNo) {
                    autoListView.setResultSize(list.size());
                    this.list.addAll(list);
                    adapter.notifyDataSetChanged();
                    pageNo += 1;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
    }

    /**
     * Starts this activity from the specified "activity".
     */
    public static void start(Activity activity, int membersNum, String membersList) {
        Intent intent = new Intent(activity, CommissionMembersActivity.class);
        intent.putExtra("members_num", membersNum);
        intent.putExtra("members_list", membersList);
        activity.startActivity(intent);
    }

}
